﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ATEGo
{
    /// <summary>
    /// 命令集文件
    /// </summary>
    public class CmdSuits : BJsonFile
    {
        /// <summary>
        /// 命令集
        /// </summary>
        public List<CmdTag> cmdSuits { get; set; }

        /// <summary>
        /// 根据cmdTagName找到对应的cmdTag
        /// </summary>
        /// <param name="cmdTagName"></param>
        /// <returns></returns>
        public CmdTag Get_cmdTag(string cmdTagName)
        {
            cmdTagName = cmdTagName.Trim();
            foreach (var item in cmdSuits)
            {
                //先找完全限定名的
                if (item.cmdTag == cmdTagName)
                {
                    return item;
                }
                //如果含有#表示真正有变量，去除变量后再找
                else if (cmdTagName.Contains("#"))
                {
                    if (cmdTagName.StartsWith(item.cmdTag))
                    {
                        return item;
                    }
                }
            }
            return null;
        }

        /// <summary>
        /// 根据cmdTag的名字，找到对应的cmdTag
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        [JsonIgnore]
        public CmdTag this[string cmdTagName]
        {
            get
            {
                return Get_cmdTag(cmdTagName);
            }
        }

        /// <summary>
        /// 获取默认的cmdTag
        /// </summary>
        [JsonIgnore]
        public CmdTag Default_cmdTag
        {
            get
            {
                if (default_cmdTag is null)
                {
                    default_cmdTag = Get_cmdTag(Name.Default_cmdTag);
                }
                return default_cmdTag;
            }
        }

        [JsonIgnore]
        private CmdTag default_cmdTag;

        /// <summary>
        /// 返回cmdSuits的个数
        /// </summary>
        [JsonIgnore]
        public int Count
        { get { return cmdSuits.Count; } }

    }

    /// <summary>
    /// 代表某个CmdTag
    /// </summary>
    public class CmdTag
    {
        /// <summary>
        /// cmdtag的名字
        /// </summary>
        public string cmdTag { get; set; }
        /// <summary>
        /// 测试结果
        /// </summary>
        public string result { get; set; }
        /// <summary>
        /// 判断条件，为false则不执行
        /// </summary>
        public string @if { get; set; }
        /// <summary>
        /// 循环
        /// 起始:结束:步长
        /// </summary>
        public string loop { get; set; }
        /// <summary>
        /// 里面包含的具体命令
        /// </summary>
        public List<CmdDict> subCmds { get; set; }

        /// <summary>
        /// 返回命令的条数
        /// </summary>
        [JsonIgnore]
        public int Count
        { get { return subCmds.Count; } }

        [JsonIgnore]
        public CmdDict this[int index]
        {
            get
            {
                return subCmds[index];
            }
        }
    }



    /// <summary>
    /// 代表某一条cmd命令，包含命令、正则、重试时间等信息
    /// </summary>
    public class CmdDict : Dictionary<string, string>
    {
        /// <summary>
        /// 默认的cmdTag
        /// </summary>
        public CmdTag Default_CmdTag { get; set; }

        [JsonIgnore]
        public string this[CmdField key]
        {
            get
            {
                return this[key.ToString()];
            }
            set
            {
                this[key.ToString()] = value;
            }
        }

        [JsonIgnore]
        public new string this[string key]
        {
            get
            {
                var result = base.TryGetValue(key, out var value);
                if (result)
                {
                    return value;
                }
                else
                {
                    try
                    {
                        return Default_CmdTag[0][key];
                    }
                    catch (Exception e)
                    {
                        MsgShow.Error($"缺少属性{key}");
                        throw e;
                    }
                    
                }
            }
            set
            {
                base[key] = value;
            }
        }

        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="default_cmdTag"></param>
        /// <returns></returns>
        public string Get(string key, CmdTag default_cmdTag)
        {
            var result = base.TryGetValue(key, out var value);
            if (result)
            {
                return value;
            }
            else
            {
                return default_cmdTag[0][key];
            }
        }
    }

    /// <summary>
    /// cmd属含有的字段名称
    /// 注意：不包含failed?Retry:GoOn
    /// </summary>
    public enum CmdField
    {
        @if,
        ifLast,
        ifReg,
        cmd,
        @params,
        finishReg,
        finishRegTimes,
        failReg,
        successReg,
        grabReg,
        grabRegTimes,
        grabParams,
        grabStdGroup,
        grabStd,
        grabJudge,
        timeOut,
        retryTimes,
        retryInterval,
        result,
        failedMsgShow,
        interval
    }
}
